/* Copyright (c) 2025 LiuYing
*    Key Laboratory of Cyberspace Security Defense,Institute of Information Engineering, CAS
*    School of Cyber Security, University of Chinese Academy of Sciences     
*
* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
*     http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/

#include<stdint.h>
#include "polarlac_param.h"
#if defined LAC_LIGHT || defined LAC128
// twiddle factors for 1024 length in naive NTT
static int16_t f[DIM_N]={
1,6531,18275,350,17782,6342,10693,11779,3828,5520,3465,12624,14860,915,11544,2894,7333,2889,2665,4363,364,17860,16220,16802,15698,17725,8171,1266,10917,83,7816,5319,11421,10633,1916,15822,11861,8725,6128,3925,14945,3060,16547,14211,3429,17137,11208,2005,9074,199,4082,5424,9819,17915,15403,8112,7500,6019,13145,7514,2245,7860,13950,11564,5329,2195,5936,3417,14658,8829,6594,5926,12514,15445,13552,11279,772,9723,7055,12138,18030,3926,8375,6414,4291,6361,4043,8777,5688,5833,4513,36,2145,18348,11317,13430,15176,215,16915,2896,512,7499,11269,13303,11345,11968,13924,7655,6038,5991,4512,11938,5587,9790,2038,1552,12597,4528,438,3463,4756,1831,4305,5630,588,6164,17694,3037,18360,2495,11534,11316,10657,16292,12030,6484,15484,2566,5117,98,2767,6937,5206,9934,17681,10299,8218,13295,10294,4963,14085,8465,15325,14818,11806,18180,14111,12374,855,17239,14185,16410,7596,6273,498,8230,13481,8403,14995,16249,8647,13278,7745,2443,11301,1099,1186,3906,15375,9574,2100,948,18427,16113,5490,3005,17364,4468,2012,16076,13898,3746,16509,5662,9064,8621,17513,638,16329,9794,8128,15361,6086,6118,17376,9108,1109,17143,10986,8330,15347,11036,118,14905,18222,4434,8735,16583,2345,15805,9312,6205,3344,14992,16565,2738,216,9788,17923,5563,6848,5830,1300,11120,15796,12608,1618,5049,2418,13310,16108,4217,17123,15735,2069,1250,4892,5263,3039,13801,17529,12969,12375,10853,17081,17928,7673,11469,4244,12765,220,17479,2106,3268,8475,14359,6559,16970,12675,16255,6547,12330,8593,10831,6348,2971,9589,8858,14877,1344,9532,5251,5450,18260,6589,10137,9619,2025,2851,2551,10367,2468,5732,16702,15994,15436,1292,14171,17060,9808,6826,9612,9039,11243,3361,15421,3519,15071,5516,6914,13256,13568,18107,9122,14642,14931,9463,15437,16352,12543,5023,12906,17418,6915,11101,3642,15610,14420,2425,3728,3943,832,6563,6228,13727,11358,4525,4676,3937,16945,3505,15802,17633,10172,13113,1285,11075,18166,16349,11383,15911,7920,4187,9158,2042,9243,2347,10434,16267,10398,9559,15691,1184,9277,7445,15474,3402,6697,12326,4195,6390,778,12562,15572,5968,9646,13881,3317,329,10471,14072,15727,7017,3589,11294,10681,3549,8238,2373,14343,12159,1065,8047,2474,451,14634,14808,11530,1327,3127,17866,1956,15854,4313,457,16954,1526,12486,4618,3770,7676,12629,16694,15752,16700,18072,13073,16540,17395,4166,5523,15765,12150,16018,16282,16198,8064,2903,17826,17211,3741,8746,12709,17113,1175,5797,2858,11402,9261,4918,5365,16115,248,16017,9655,15945,4449,6011,1981,16378,363,11329,679,10629,3316,16454,7305,4351,7089,12996,159,6181,11744,351,1469,8879,7527,16459,2197,7733,3101,13197,1267,16793,2577,1058,4668,16959,18209,11696,7710,13487,16831,7282,12999,12504,10654,15132,2647,15836,5733,4800,9505,13244,9716,8810,3419,7126,12788,16938,4624,6090,6728,14729,502,15921,12849,9803,4992,13208,3883,14498};
static int16_t fn[DIM_N]={
1,11902,18083,158,6654,7740,12091,651,15539,6889,17518,3573,5809,14968,12913,14605,13114,10617,18350,7516,17167,10262,708,2735,1631,2213,573,18069,14070,15768,15544,11100,6869,4483,10573,16188,10919,5288,12414,10933,10321,3030,518,8614,13009,14351,18234,9359,16428,7225,1296,15004,4222,1886,15373,3488,14508,12305,9708,6572,2611,16517,7800,7012,15396,739,12269,17845,12803,14128,16602,13677,14970,17995,13905,5836,16881,16395,8643,12846,6495,13921,12442,12395,10778,4509,6465,7088,5130,7164,10934,17921,15537,1518,18218,3257,5003,7116,85,16288,18397,13920,12600,12745,9656,14390,12072,14142,12019,10058,14507,403,6295,11378,8710,17661,7154,4881,2988,5919,12507,11839,9604,3775,15016,12497,16238,13104,505,1352,7580,6058,5464,904,4632,15394,13170,13541,17183,16364,2698,1310,14216,2325,5123,16015,13384,16815,5825,2637,7313,17133,12603,11585,12870,510,8645,18217,15695,1868,3441,15089,12228,9121,2628,16088,1850,9698,13999,211,3528,18315,7397,3086,10103,7447,1290,17324,9325,1057,12315,12347,3072,10305,8639,2104,17795,920,9812,9369,12771,1924,14687,4535,2357,16421,13965,1069,15428,12943,2320,6,17485,16333,8859,3058,14527,17247,17334,7132,15990,10688,5155,9786,2184,3438,10030,4952,10203,17935,12160,10837,2023,4248,1194,17578,6059,4322,253,6627,3615,3108,9968,4348,13470,8139,5138,10215,8134,752,8499,13227,11496,15666,18335,13316,15867,2949,11949,6403,2141,7776,7117,6899,15938,73,3935,14550,5225,13441,8630,5584,2512,17931,3704,11705,12343,13809,1495,5645,11307,15014,9623,8717,5189,8928,13633,12700,2597,15786,3301,7779,5929,5434,11151,1602,4946,10723,6737,224,1474,13765,17375,15856,1640,17166,5236,15332,10700,16236,1974,10906,9554,16964,18082,6689,12252,18274,5437,11344,14082,11128,1979,15117,7804,17754,7104,18070,2055,16452,12422,13984,2488,8778,2416,18185,2318,13068,13515,9172,7031,15575,12636,17258,1320,5724,9687,14692,1222,607,15530,10369,2235,2151,2415,6283,2668,12910,14267,1038,1893,5360,361,1733,2681,1739,5804,10757,14663,13815,5947,16907,1479,17976,14120,2579,16477,567,15306,17106,6903,3625,3799,17982,15959,10386,17368,6274,4090,16060,10195,14884,7752,7139,14844,11416,2706,4361,7962,18104,15116,4552,8787,12465,2861,5871,17655,12043,14238,6107,11736,15031,2959,10988,9156,17249,2742,8874,8035,2166,7999,16086,9190,16391,9275,14246,10513,2522,7050,2084,267,7358,17148,5320,8261,800,2631,14928,1488,14496,13757,13908,7075,4706,12205,11870,17601,14490,14705,16008,4013,2823,14791,7332,11518,1015,5527,13410,5890,2081,2996,8970,3502,3791,9311,326,4865,5177,11519,12917,3362,14914,3012,15072,7190,9394,8821,11607,8625,1373,4262,17141,2997,2439,1731,12701,15965,8066,15882,15582,16408,8814,8296,11844,173,12983,13182,8901,17089,3556,9575,8844,15462,12085,7602,9840,6103,11886,2178,5758,1463,11874,4074,9958,15165,16327,954,18213,5668,14189,6964,10760};

// twiddle factors for 1024 length in NTT use Montgomery
static int16_t M[DIM_N]={
10237,1356,4658,6948,8459,2028,9087,11370,17111,11095,6113,16558,12704,2891,1965,4047,8745,8161,765,872,2802,14326,18109,3751,1532,14806,16006,1643,16483,1753,13172,17954,14691,3156,1380,17476,2886,9940,4837,14718,16498,7553,10802,4771,6241,4608,9304,9256,6651,9533,18256,5292,1954,5938,4629,1879,4055,13417,4465,18342,14547,2775,5699,3942,9726,388,11664,12428,9326,5474,1132,1459,14901,10624,5066,17244,13640,14584,1541,18286,3481,6522,2992,1772,1128,12201,6106,7707,16642,7934,6483,18305,4662,14639,724,9596,3388,7428,17686,6088,6372,12251,7039,18240,10865,10698,16032,5552,5157,3276,14679,16949,14953,9,15283,17011,16654,12574,4587,4072,5619,16019,15415,12752,10198,4709,10820,11731,8452,11610,10193,8920,9215,17853,237,17908,4341,1117,14576,7844,12691,10153,4019,17930,6770,12536,17887,10076,16650,4883,5219,2672,17195,6709,11274,9092,13319,1062,15393,16534,15104,9241,9858,14562,10518,11700,15559,13133,12224,1621,4073,1944,5232,13843,2829,6333,12168,4545,12921,777,4822,8918,12310,10297,17346,15941,5849,6643,7183,188,7932,7162,8889,8542,14879,14406,1223,5924,9529,4091,18207,17067,17275,13065,18095,4482,16538,10731,3949,3152,2780,18108,9821,12544,15087,8812,1712,10674,5999,9344,9901,467,2447,18379,10738,10746,17665,16401,14102,8894,2277,14089,17907,11665,9376,230,10632,481,15980,16167,14411,17776,8754,11641,836,3748,15376,16105,13772,10325,17551,9187,11299,6470,2759,9988,5488,8376,17680,3768,3314,3392,10945,17054,12877,8341,11497,9298,4088,7744,17684,11459,4265,2452,8151,18110,6868,7419,2403,7510,13215,3859,13392,17000,5246,13112,617,11233,6248,13459,8198,11706,6245,12299,8672,10656,9743,817,8978,18378,16692,2690,17016,17372,10579,4565,5921,16050,7013,14331,16359,2961,17544,336,11431,2211,7316,2460,5351,16846,10814,9411,5657,6135,1492,11628,3893,6076,13907,7226,14654,1138,15579,14722,8540,14915,296,16144,8531,11435,10067,15499,13085,2747,8675,11816,11825,13238,11506,12778,6919,8706,5594,208,932,4002,8040,12056,1557,12184,13119,3605,10127,1833,12443,12569,6337,4862,7377,13758,14146,1330,8586,1980,7454,521,18233,2543,13167,3732,1169,3477,18061,3624,4902,15274,18103,1431,16140,10446,12067,8502,62,17829,8637,3267,14937,6111,17811,11411,2016,5334,13266,5146,14760,11403,8911,4760,12254,13221,17766,12444,4135,1340,10258,9476,4721,12875,9835,11813,4940,5390,12099,14731,7648,14091,8194,4015,16495,6393,11276,3721,1919,16982,10159,8062,4175,4518,3938,5043,9598,12338,13455,4494,489,4750,14903,5253,3197,13451,10998,12970,1682,17507,10739,17277,17037,7059,17805,9091,5579,12841,3302,17185,15258,1200,3959,13163,2429,11419,3311,2232,11880,3783,3126,10575,7980,7289,11037,9617,15597,3249,5696,2782,2936,4696,15370,13785,829,13330,16482,13655,13311,4113,16657,13734,14469,9481,18023,13508,18377,2924,8848,17266,14600,17124,15758,4059,6828,4241,8723,11943};
static int16_t Mn[DIM_N]={
10237,17077,11485,13775,7063,9346,16405,9974,14386,16468,15542,5729,1875,12320,7338,1322,479,5261,16680,1950,16790,2427,3627,16901,14682,324,4107,15631,17561,17668,10272,9688,14491,12734,15658,3886,91,13968,5016,14378,16554,13804,12495,16479,13141,177,8900,11782,9177,9129,13825,12192,13662,7631,10880,1935,3715,13596,8493,15547,957,17053,15277,3742,6702,7613,13724,8235,5681,3018,2414,12814,14361,13846,5859,1779,1422,3150,18424,3480,1484,3754,15157,13276,12881,2401,7735,7568,193,11394,6182,12061,12345,747,11005,15045,8837,17709,3794,13771,128,11950,10499,1791,10726,12327,6232,17305,16661,15441,11911,14952,147,16892,3849,4793,1189,13367,7809,3532,16974,17301,12959,9107,6005,6769,18045,8707,8445,15674,11963,7134,9246,882,8108,4661,2328,3057,14685,17597,6792,9679,657,4022,2266,2453,17952,7801,18203,9057,6768,526,4344,16156,9539,4331,2032,768,7687,7695,54,15986,17966,8532,9089,12434,7759,16721,9621,3346,5889,8612,325,15653,15281,14484,7702,1895,13951,338,5368,1158,1366,226,14342,8904,12509,17210,4027,3554,9891,9544,11271,10501,18245,11250,11790,12584,2492,1087,8136,6123,9515,13611,17656,5512,13888,6265,12100,15604,4590,13201,16489,14360,16812,6209,5300,2874,6733,7915,3871,8575,9192,3329,1899,3040,17371,5114,9341,7159,11724,1238,15761,13214,13550,1783,8357,546,5897,11663,503,14414,8280,5742,10589,3857,17316,14092,525,18196,580,9218,9513,8240,6823,9981,6490,9710,14192,11605,14374,2675,1309,3833,1167,9585,15509,56,4925,410,8952,3964,4699,1776,14320,5122,4778,1951,5103,17604,4648,3063,13737,15497,15651,12737,15184,2836,8816,7396,11144,10453,7858,15307,14650,6553,16201,15122,7014,16004,5270,14474,17233,3175,1248,15131,5592,12854,9342,628,11374,1396,1156,7694,926,16751,5463,7435,4982,15236,13180,3530,13683,17944,13939,4978,6095,8835,13390,14495,13915,14258,10371,8274,1451,16514,14712,7157,12040,1938,14418,10239,4342,10785,3702,6334,13043,13493,6620,8598,5558,13712,8957,8175,17093,14298,5989,667,5212,6179,13673,9522,7030,3673,13287,5167,13099,16417,7022,622,12322,3496,15166,9796,604,18371,9931,6366,7987,2293,17002,330,3159,13531,14809,372,14956,17264,14701,5266,15890,200,17912,10979,16453,9847,17103,4287,4675,11056,13571,12096,5864,5990,16600,8306,14828,5314,6249,16876,6377,10393,14431,17501,18225,12839,9727,11514,5655,6927,5195,6608,6617,9758,15686,5348,2934,8366,6998,9902,2289,18137,3518,9893,3711,2854,17295,3779,11207,4526,12357,14540,6805,16941,12298,12776,9022,7619,1587,13082,15973,11117,16222,7002,18097,889,15472,2074,4102,11420,2383,12512,13868,7854,1061,1417,15743,1741,55,9455,17616,8690,7777,9761,6134,12188,6727,10235,4974,12185,7200,17816,5321,13187,1433,5041,14574,5218,10923,16030,11014,11565,323,10282,15981,14168,6974,749,10689,14345,9135,6936,10092,5556,1379,7488,15041,15119,14665,753,10057,12945};

#endif